﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Services.Client;
using TelerikStore.EbayService;
using Telerik.Web.UI;
using TelerikStore.MasterPages;
using System.Web.Services;

namespace TelerikStore
{
    public partial class ShoppingCartView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadAjaxManager manager = RadAjaxManager.GetCurrent(Page);
            manager.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(RadAjaxManager1_AjaxRequest);

            string script = "(function() { $telerik.$('li',$telerik.$('nav')).removeClass('current');  $telerik.$('#cart',$telerik.$('nav')).parent().addClass('current'); }());";
            ScriptManager.RegisterStartupScript(Page, GetType(), "SetPane", script, true);

            RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(ShoppingCartGrid, Page.Master.Master.FindControl("CartRotator"));
            RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(ShoppingCartGrid, Page.Master.Master.FindControl("ItemsTotalLabel"));
            RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(ShoppingCartGrid, Page.Master.Master.FindControl("PriceTotalLabel"));
            RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(Page.Master.Master.FindControl("CartRotator"), ShoppingCartGrid);
        }

        //Redirects to the Category View page when an items from the menu is clicked.
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            Session.Remove("Products");
            String[] arguments = e.Argument.Split(",".ToCharArray());
            if (arguments[0] == "Category")
            {
                Response.RedirectLocation = "./CategoryView.aspx?Category=" + arguments[1];
            }
        }
        protected void PerformSearch(object sender, EventArgs e)
        {
            Session.Remove("Products");
            Response.Redirect("./CategoryView.aspx?Search=" + SearchBox.Text);
        }

        protected void ItemQuantityTextBox_TextChanged(object sender, EventArgs e)
        {
            var homeMaster = Master.Master as Home;
            var shoppingCartProducts = homeMaster.CurrentCart.Items;
            RadNumericTextBox numericBox = sender as RadNumericTextBox;
            GridDataItem item = numericBox.NamingContainer as GridDataItem;
            var itemId = item.GetDataKeyValue("Id").ToString();
            ProductItem updatedItem = shoppingCartProducts.Single(x => x.Id == itemId);

            updatedItem.Quantity = Convert.ToInt32(numericBox.DbValue);
            ShoppingCartGrid.Rebind();
        }

        protected void ShoppingCartGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            ShoppingCartGrid.DataSource = (Master.Master as Home).CurrentCart.Items;
        }

        protected void EmptyCartButton_Click(object sender, EventArgs e)
        {
            (Master.Master as Home).CurrentCart.Empty();
            ShoppingCartGrid.Rebind();
        }

        protected void BuyButton_Click(object sender, EventArgs e)
        {
            var homeMaster = Master.Master as Home;
            homeMaster.CurrentCart.Empty();
            homeMaster.UpdateShoppingCart();

            Response.Redirect("./About.aspx");
        }
    }
}
